using Services;
using Services.Event;
using Services.SceneManagement;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

public class GameManager : Service, IService
{
    public UnityAction<bool> AfterGameStateChange;

    public override Type RegisterType => GetType();

    [AutoService]
    private IEventSystem eventSystem;

    public EGameMode gameMode;
    public bool computerFirst;
    public bool isPlaying;
    public float decisionTime;

    [SerializeField]
    private float restartTime;

    public void ChangeGameState(bool isPlaying)
    {
        this.isPlaying = isPlaying;
        eventSystem.Invoke(EEvent.AfterGameStateChange, isPlaying);
        if (!isPlaying)
            StartCoroutine(RestartProcess());
    }

    private IEnumerator RestartProcess()
    {
        yield return new WaitForSeconds(restartTime);
        ChangeGameState(true);
    }

    private void AfterLoadScene(int index)
    {
        if (SceneControllerUtility.ToSceneName(index) == "1")
        {
            ChangeGameState(true);
        }
    }


    protected override void Awake()
    {
        base.Awake();
        Application.targetFrameRate = -1;
        Screen.SetResolution(1920, 1080, true);
    }

    protected internal override void Init()
    {
        base.Init();
        eventSystem.AddListener<int>(EEvent.AfterLoadScene, AfterLoadScene);
    }

    private void OnDestroy()
    {
        eventSystem.RemoveListener<int>(EEvent.AfterLoadScene, AfterLoadScene);
    }
}
